package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class RegisterVisitorServlet
 */
@WebServlet("/RegisterVisitorServlet")
public class RegisterVisitorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterVisitorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		VisitorService visitorS=new VisitorService();
		
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		
		String fname=request.getParameter("nombre");
		String lname=request.getParameter("apell");
		String username=request.getParameter("visitor");
		String email=request.getParameter("email");
		String dni=request.getParameter("dni");
		String nphone=request.getParameter("phone");
		String address=request.getParameter("adress");
		String pass=request.getParameter("pass1");
		
		Visitor visitor=new Visitor(fname,lname,username,email,dni,nphone,address,pass, false);
		int i=visitorS.insertVisitorService(visitor);
		
		if(i==1){
			/*Cookie loginCookie = new Cookie("user", username);
			loginCookie.setMaxAge(30*60);
			response.addCookie(loginCookie);*/
			RequestDispatcher rd= request.getRequestDispatcher("index.jsp");
			rd.forward(request, response);
		}
		else {
			out.print("That user already exists.");
			RequestDispatcher rd= request.getRequestDispatcher("visitorRegister.jsp");
			rd.include(request, response);
		}
	out.close();
	}

}
